﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SQLite;
using System.Windows;

namespace MOJsdk
{
    public class MojSystem
    {
        string _localFolder, _languageFolder, _pluginFolder, _skinFolder, _tempFolder, _DataBase, _imageFolder, _logFolder;
        string _langXml, _skin;
        IHost _host;

        public MojSystem(string ApplicationPath, IHost TheHost)
        {
            _localFolder = ApplicationPath;
            _host = TheHost;
            INIDataBase();
            INIFolders();
            INIAppVars();
        }

        private void INIDataBase()
        {
            _DataBase = Path.Combine(_localFolder, "MOJData.db");

            using (SQLiteConnection _sqliteConnection = new SQLiteConnection(_DataBase))
            {
                _sqliteConnection.CreateTable<VarStore>();
                _sqliteConnection.CreateTable<MOJBasicMovieClass>();
                _sqliteConnection.CreateTable<MOJBasicFileClass>();
                _sqliteConnection.CreateTable<ISO639_1>();

                if (_sqliteConnection.Table<ISO639_1>().Count() < 180)
                {
                    foreach (var v in ISO639_1.GetLanguages())
                    {
                        try { _sqliteConnection.Insert(v); }
                        catch { }
                    }
                }
            }
        }
        private void INIFolders()
        {
            //Test if Language Folder is valid
            if (VarExist("MOJLanguageFolder") && Directory.Exists(GetVar("MOJLanguageFolder")))
                _languageFolder = GetVar("MOJLanguageFolder");
            else
                _languageFolder = Path.Combine(_localFolder, "Languages");
            SetVar("MOJLanguageFolder", _languageFolder);

            //Test if Plugin Folder is valid
            if (VarExist("MOJPluginFolder") && Directory.Exists(GetVar("MOJPluginFolder")))
                _pluginFolder = GetVar("MOJPluginFolder");
            else
                _pluginFolder = Path.Combine(_localFolder, "Plugins");
            SetVar("MOJPluginFolder", _pluginFolder);

            //Test if Skin Folder is valid
            if (VarExist("MOJSkinFolder") && Directory.Exists(GetVar("MOJSkinFolder")))
                _skinFolder = GetVar("MOJSkinFolder");
            else
                _skinFolder = Path.Combine(_localFolder, "Styles");
            SetVar("MOJSkinFolder", _skinFolder);

            //Test if Image Folder is valid
            if (VarExist("MOJImagesFolder") && Directory.Exists(GetVar("MOJImagesFolder")))
                _imageFolder = GetVar("MOJImagesFolder");
            else
                _imageFolder = Path.Combine(_localFolder, "Images");
            SetVar("MOJImagesFolder", _imageFolder);

            //Test if Temp Folder is valid
            if (VarExist("MOJTempFolder") && Directory.Exists(GetVar("MOJTempFolder")))
                _tempFolder = GetVar("MOJTempFolder");
            else
                _tempFolder = Path.Combine(_localFolder, "Temp");
            SetVar("MOJTempFolder", _tempFolder);

            //Test if Log Folder is valid
            if (VarExist("MOJLogFolder") && Directory.Exists(GetVar("MOJTempFolder")))
                _logFolder = GetVar("MOJLogFolder");
            else
                _logFolder = Path.Combine(_localFolder, "LOG");
            SetVar("MOJLogFolder", _logFolder);

            if (!Directory.Exists(_languageFolder))
                Directory.CreateDirectory(_languageFolder);

            if (!Directory.Exists(_pluginFolder))
                Directory.CreateDirectory(_pluginFolder);

            if (!Directory.Exists(_skinFolder))
                Directory.CreateDirectory(_skinFolder);

            if (!Directory.Exists(_imageFolder))
                Directory.CreateDirectory(_imageFolder);

            if (!Directory.Exists(_logFolder))
                Directory.CreateDirectory(_logFolder);

            if (Directory.Exists(_tempFolder))
                try { Directory.Delete(_tempFolder, true); }
                catch { }
            Directory.CreateDirectory(_tempFolder);
        }
        private void INIAppVars()
        {
            if (VarExist("MOJLanguage"))
                _langXml = GetVar("MOJLanguage");
            else
                _langXml = "DEFAULT";
            SetVar("MOJLanguage", _langXml);

            if (VarExist("MOJSkin"))
                _skin = GetVar("MOJSkin");
            else
                _skin = "MoJBlack";
            SetVar("MOJSkin", _skin);
        }

        #region VarStore
        public bool VarExist(string Key)
        {
            using (SQLiteConnection _sqliteConnection = new SQLiteConnection(_DataBase))
            {
                 return _sqliteConnection.Table<VarStore>().Where(data => data.Key == Key).Count() == 1;
            }
        }
        public void SetVar(string Key, string Value)
        {
            using (SQLiteConnection _sqliteConnection = new SQLiteConnection(_DataBase))
            {
                if (VarExist(Key))
                {
                    VarStore s = new VarStore();
                    s.Key = Key;
                    s.Value = Value;
                    _sqliteConnection.Update(s, typeof(VarStore));
                }
                else
                    _sqliteConnection.Insert(new VarStore() { Key = Key, Value = Value, });
            }
        }
        public string GetVar(string Key)
        {
            if (!VarExist(Key))
                return null;

            using (SQLiteConnection _sqliteConnection = new SQLiteConnection(_DataBase))
            {
                return _sqliteConnection.Table<VarStore>().First(data => data.Key == Key).Value;
            }
        }
        #endregion

        #region PublicVars
        public string AppLocal
        { get { return _localFolder; } }
        public string LanguageFolder
        { get { return GetVar("MOJLanguageFolder"); } }
        public string PluginFolders
        { get { return GetVar("MOJPluginFolder"); } }
        public string StyleFolder
        { get { return GetVar("MOJSkinFolder"); } }
        public string TempFolder
        { get { return GetVar("MOJTempFolder"); } }
        public string ImagesFolder
        { get { return GetVar("MOJImagesFolder"); } }
        public string DataBaseLocal
        { get { return _DataBase; } }
        public string LogFolder
        { get { return GetVar("MOJLogFolder"); } }

        public string LanguageFile
        { get { return GetVar("MOJLanguage"); } }
        public string Style
        { get { return GetVar("MOJSkin"); } }
        #endregion
    }
}
